package tinybase.rm;
import junit.framework.Assert;
import tinybase.basic.RC;
import tinybase.basic.BytePointer;
import tinybase.basic.RedBase;
import tinybase.pf.PageNum;
import tinybase.pf.Slot;

// RM_Record
public class RM_Record {
	private byte[] pData;
	public byte[] getpData() {
		return pData;
	}

	private int recordSize;
	private RID rid;
	
	//Default Constructor 
	public RM_Record() {
		pData = null;
		recordSize = 0;
		rid = new RID();
	}
	
	//Constructor
	public RM_Record(byte[] _pData, int _recordSize, int pageNum, int slotNum){
		pData = _pData;
		recordSize = _recordSize;
		rid = new RID(pageNum,slotNum);
	}
	
    //Donner pData la valeur
	public void setpData(byte[] pData) {
		this.pData = pData;
	}
	
	// Récupérer la valeur de pData
	public RC getData(BytePointer bytePointer) {
		if (this.pData == null)
			return RC.RM_UNREADRECORD;
		//bytePointer = new BytePointer(this.pData);
		bytePointer.setArray(pData);
		return RC.SUCCESS;
		
	}
    
	// Donner recordSize la valeur
	public void setRecordSize(int recordSize) {
		this.recordSize = recordSize;
	}
	
	// Récupérer la valeur de recordSize
	public int getRecordSize() {
		return recordSize;
	}
	
	// Donner rid la valeur
	public void setRid(RID rid) {
		this.rid = rid;
	}

	// Récupérer la valeur de rid
	public RC getRid(RID _rid) {
		Assert.assertTrue(_rid != null);
		int pn,sn;
		PageNum refPageNum = new PageNum(PageNum.PF_PAGE_UNSET);
		Slot refSlot = new Slot(Slot.SLOT_UNSET);
		if (this.pData == null)
			return RC.RM_UNREADRECORD;
		RC rc1 = rid.getPageNum(refPageNum);
		if (rc1 == null)
			return rc1;
		pn = refPageNum.getPageNum();
		RC rc2 = rid.getSlotNum(refSlot);
		if (rc2 == null)
			return rc2;
		sn = refSlot.getSlotValue();
		_rid.setPageNum(pn);
		_rid.setSlotNum(sn);
		return RC.SUCCESS;
	}
}
